#!/system/bin/sh
#########################################################################
#	CameraTest
#	adb push CameraTest /data/data
#	adb pull sdcard/DCIM/Camera
#
#   Command
#   adb shell data/data/CameraTest PARA1 PARA2 PARA3
#
#########################################################################

TEST_TIMES=-1
INDEX=0
COUNT=10

# change REC to 0 until the apk set flag finished
REC=1
#REC=0

case $1 in
	"0")
		# enable/disable camera preview

		case $2 in
		"0")
			# close camera preview
			
			# clear flag
			setprop sys.camera.atd.cameraindex 0
			#setprop sys.camera.atd.capture.size 0
			#setprop sys.camera.atd.rotation -1
			setprop sys.camera.atd.flashmode -1
			setprop sys.camera.atd.jpg.filename null

			# set camera close flag to 0, wait until camera apk set to 1
			setprop sys.camera.atd.close 0

			# stop the preview through the keyevent way
			# sh /system/bin/input keyevent 4

			sh /system/bin/am force-stop com.android.gallery3d

			while [ $INDEX -lt $3 ]
			do
				INDEX=$(($INDEX+1))
				result=`getprop sys.camera.atd.close`
				if [ $result == $REC ]; then
					# echo "Close camera success!!"
					echo "1"
					exit 0
				fi
				sleep 1
			done
			# echo "Close camera failed!!"
			echo "0"

		;;
		"1")
			# Preparing to open back camera preview

			# set camera index to open back camera
			setprop sys.camera.atd.cameraindex b

			# back camera no need to do the flash
			setprop sys.camera.atd.flashmode off

			# set back camera picture size qvga
			#setprop sys.camera.atd.capture.size qvga

			# set camera start flag to 0, until camera apk set to 1
			setprop sys.camera.atd.open 0

			# start the preview
			# hw camera apk
			# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

			# qualcomm apk
			sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

			while [ $INDEX -lt $3 ]
			do
				INDEX=$(($INDEX+1))
				result=`getprop sys.camera.atd.open`
				if [ $result == $REC ]; then
					# echo "open camera success!!"
					echo "1"
					exit 0
				fi
				sleep 1
			done
			# echo "open camera failed!!"
			echo "0"
		;;
		*)
			echo "Invalid argument!!(para2 : onoffID = $2)"
		;;
		esac
	;;
	"1")
		# open/close front camera preview
		case $2 in
		"0")
			# close front camera preview

			# clear flag
			setprop sys.camera.atd.cameraindex 0
			#setprop sys.camera.atd.capture.size 0
			#setprop sys.camera.atd.rotation -1

			setprop sys.camera.atd.jpg.filename null

			# set camera start flag to 0, wait until camera apk set to 1
			setprop sys.camera.atd.close 0

			# stop the preview
			# sh /system/bin/input keyevent 4
			sh /system/bin/am force-stop com.android.gallery3d

			while [ $INDEX -lt $3 ]
			do
				INDEX=$(($INDEX+1))
				result=`getprop sys.camera.atd.close`
				if [ $result == $REC ]; then
					# echo "Close camera success!!"
					echo "1"
					exit 0
				fi
				sleep 1
			done
			# echo "Close camera failed!!"
			echo "0"
		;;
		"1")
			# set open front camera
			setprop sys.camera.atd.cameraindex f
			
			# set front camera picture size vga
			#setprop sys.camera.atd.capture.size vga
			
			# set camera start flag to 0, until camera apk set to 1
			setprop sys.camera.atd.open 0

			# start the preview
			# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

			sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

			while [ $INDEX -lt $3 ]
			do
				INDEX=$(($INDEX+1))
				result=`getprop sys.camera.atd.open`
				if [ $result == $REC ]; then
					# echo "open camera success!!"
					echo "1"
					exit 0
				fi
				sleep 1
			done

			# echo "open camera failed!!"
			echo "0"
		;;
		*)
			echo "Invalid argument!!(para2 : onoffID = $2)"
		;;
		esac
	;;
	"2")
		# take picture

		# set sys node
		setprop sys.camera.atd.jpg.filename $2

		# set rotation
		#setprop sys.camera.atd.rotation 180

		# set camera capture flag to 0, until camera apk set to 1
		setprop sys.camera.atd.capture 0

		# start the preview
		# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

		# if capture after camera preview done, we can remove this am start
		# sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

		# tap to make the manul focus
		# for the QL810 tp size
		/system/bin/input tap 360 640

		sleep 1

		# take picture
		/system/bin/input keyevent 27

		while [ $INDEX -lt $3 ]
		do
			INDEX=$(($INDEX+1))
			result=`getprop sys.camera.atd.capture`
			if [ $result == $REC ]; then
				# echo "Picture taken success!!"

				# sleep to finish the jpeg process
				sleep 2

				echo "1"
				exit
			fi
			sleep 1
		done
		# echo "Picture taken failed!!"
		echo "0"
	;;


	"3")
		# set af mode
		case $2 in
		"0")
			# set sys node
			#setprop sys.camera.atd.afmode 0

			# start the preview
			# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

			# if do this after camera preview done, we can remove this am start
			# sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

			# sleep 1

			echo "1"
			exit
		;;
		"1")
			# set sys node
			#setprop sys.camera.atd.afmode 1

			# start the preview
			# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

			# if do this after camera preview done, we can remove this am start
			sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

			#sleep 1

			echo "1"
			exit
		;;
		*)
			echo "Invalid argument!!(para2 : onoffID = $2)"
		;;
		esac
	;;

	"4")
		# take picture for front camera in

		# set sys node
		setprop sys.camera.atd.jpg.filename $2

		# set rotation
		#setprop sys.camera.atd.rotation 180

		# set camera capture flag to 0, until camera apk set to 1
		setprop sys.camera.atd.capture 0

		# start the preview
		# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

		# if do this after camera preview done, we can remove this am start
		# sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

		# take picture
		/system/bin/input keyevent 27

		while [ $INDEX -lt $3 ]
		do
			INDEX=$(($INDEX+1))
			result=`getprop sys.camera.atd.capture`
			if [ $result == $REC ]; then
				# echo "Picture taken success!!"

				# sleep to finish the jpeg process
				sleep 2

				echo "1"
				exit
			fi
			sleep 1
		done
		# echo "Picture taken failed!!"
		echo "0"
	;;

	"5")
		# take raw picture

		# set sys node
		#setprop sys.camera.raw.filename $2
		#setprop sys.camera.raw 1

		# set raw capture flag to 0, until camera apk set to 1
		setprop sys.camera.rawcapture 0

		# start the preview
		# sh /system/bin/am start -n com.android.gallery3d/com.android.hwcamera

		# if do this after camera preview done, we can remove this am start
		# sh /system/bin/am start -n com.android.gallery3d/com.android.camera.CameraActivity

		# take picture
		input keyevent 27

		while [ $INDEX -lt $3 ]
		do
			INDEX=$(($INDEX+1))
			result=`getprop sys.camera.rawcapture`
			if [ $result == $REC ]; then
				# echo "RAW Picture taken success!!"
				echo "1"
				exit
			fi
			sleep 1
		done
		# echo "RAW Picture taken failed!!"
		echo "0"
	;;

	"6")
		#echo "Calibration!"
		echo " need to do Calibration!"
		sleep 1
		echo "0"
	;;

	"7")
		#echo "Get golden data!"
		echo "r/g = 0x400; b/g = 0x400; g_ave = 0x400"
		sleep 1
		echo "0"
	;;

	"8")
		#echo "Get OTP data!"
		cat /sys/bus/platform/drivers/qcom,eeprom/6b.qcom,eeprom/raw_data
		sleep 1
		echo "0"
	;;

	"9")
		#echo "clear all the sys flags"
		setprop sys.camera.atd.cameraindex 0

		setprop sys.camera.atd.jpg.filename null

		setprop sys.camera.atd.open 0
		setprop sys.camera.atd.close 0
		setprop sys.camera.atd.capture 0
		setprop sys.camera.atd.flashmode -1

		#setprop sys.camera.atd.capture.size -1
		#setprop sys.camera.rawcapture 0
		#setprop sys.camera.atd.rotation -1

		echo "all flags cleared"
		exit 0

	;;
esac

exit 0
